package com.tangyee.utils;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author leeguiyang@yeah.net
 * 这个类用于Session的过程处理，主要是登录注册模块，
 */
public class SessionUtil implements SessionHelper {

    public static String getLoginHint(int loginState) {
        switch (loginState) {
            case LOGINSUCCESSFUL:
                return "登录成功";
            case LOGINNOEXISTUSER:
                return "用户不存在,请注册";
            case LOGININFOMISMATCH:
                return "账号与密码出错";
        }
        return null;
    }

    public static String getLoginHint(String jsonStr) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return getLoginHint(jsonObject.getInt(STATE));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getRegisterHint(int registerState) {
        if (registerState == REGISTERED) {
            return "已经注册";
        } else {
            return "注册成功";
        }
    }

    public static String getRegisterHint(String jsonStr) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            if (jsonObject.getInt(STATE) >= 0) {
                return getRegisterHint(jsonObject.getInt(STATE));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //登录和注册时，成功都是4
    public static boolean isSuccessful(String jsonStr) {
        try {
            Log.d("IsSuccessful", "判断是否成功时" + jsonStr);
            JSONObject json = new JSONObject(jsonStr);
            Log.d(TAG, "state = " + json.getInt(STATE));
            return isSuccessful(json.getInt(STATE));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean isSuccessful(int state) {
        if (state == LOGINSUCCESSFUL) {
            return true;
        }
        return false;
    }

    public static boolean isPhonenumber(String phonenumber) {
        String phoneString = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";
        boolean result = false;
        if (!TextUtils.isEmpty(phonenumber)) {
            result = phonenumber.matches(phoneString);
            Log.d("测试手机号码", "" + result);
        }
        return result;
    }

    private static final String TAG = "SessionUtil";


    @Override
    public final void registerSessionReceiver() {
    }

    @Override
    public final void unregisterSessionReceiver() {
    }
}
